package cn.weiron.fun.schedule;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * @author weiRon
 * @date 2022/9/27 17:14
 */
public class ImageUtil {

//    public static void main(String[] args) throws IOException {
//
//
//
//    }
    public static void createCode(String backgroundPath,String codePath,String pointName,String address,String name,String savePath)throws IOException{
        File file = new File(savePath);
        if(!file.exists()){
            file.mkdirs();
        }
//        BufferedImage im = ImageIO.read(new File("E:\\testData\\点位二维码1.jpg"));
        BufferedImage im = ImageIO.read(new File(backgroundPath));
//        BufferedImage im2 = ImageIO.read(new File("E:\\testData\\0.JPG"));
        BufferedImage im2 = ImageIO.read(new File(codePath));
        Graphics2D g = im.createGraphics();
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));
        g.drawImage(im2, 400,600, 1200,1200,null);
        //设置颜色。
        g.setColor(Color.BLACK);
        //最后一个参数用来设置字体的大小
        Font f = new Font("宋体", Font.PLAIN, 50);
        Color mycolor = Color.BLACK;//new Color(0, 0, 255);
        g.setColor(mycolor);
        g.setFont(f);
        g.drawString("点位名字："+pointName, 300, 2100);
        g.drawString("点位地址："+address, 300, 2200);
        g.dispose();
        display(im);
//        ImageIO.write(im, "jpeg", new File("E:\\testData\\123.jpg"));

        ImageIO.write(im, "jpeg", file);
        im.flush();
        im2.flush();
    }

    private static void display(BufferedImage image) {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(new JLabel(new ImageIcon(image)));
        f.pack();
        f.setVisible(false);
    }

}
